package Main.udp;

import Main.tool.ByteTool;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: twobox
 * Date: 2020/3/14
 * Time: 20:20
 * Description: No Description
 */
public class UDPTest2 {
    static private String targetIp;
    static private int targetPort;
    static private Scanner scanner = new Scanner(System.in);


    public static void main(String[] args) throws Exception {
        // 创建一个UDPSocket
        DatagramSocket udpSocket = new DatagramSocket(8889);
        // 开启线程接受信息
        new Thread(new DoReceive(udpSocket)).start();
        // 开启指令操作
        System.out.println("已开启线程接受消息！请输入指令(0)：");
        int n = -1;
        while ((n = scanner.nextInt()) != -1) {
            switch (n) {
                case 0:
                    System.out.println("0：帮助信息\n1：发送消息到Ojbk\n2：设定目标IP与端口\n3：发送消息到目标主机\n");
                    break;
                case 1:
                    sendToOjbk(udpSocket);
                    break;
                case 2:
                    getTarget();
                    break;
                case 3:
                    sentToComputer(udpSocket);
            }
        }
    }

    public static void sentToComputer(DatagramSocket socket) throws IOException {
        socket.send(new DatagramPacket(new byte[1], 0, 1, InetAddress.getByName(targetIp), targetPort));
    }

    public static void getTarget() {
        System.out.println("请输入IP与地址:");
        targetIp = scanner.next();
        targetPort = scanner.nextInt();
        System.out.println("设置成功" + targetIp + ":" + targetPort);
    }

    public static void sendToOjbk(DatagramSocket udpSocket) throws IOException {
        // 准备一个要发送的Packet
        DatagramPacket needSendPacket = new DatagramPacket(new byte[0], 0, 0, InetAddress.getByName("ojbk.net"), 8888);
        udpSocket.send(needSendPacket);
    }

    static class DoReceive implements Runnable {
        private DatagramSocket socket;

        public DoReceive(DatagramSocket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            DatagramPacket packet = new DatagramPacket(new byte[100], 100);
            try {
                while (true) {
                    socket.receive(packet);
                    if (packet.getLength() >= 10) {
                        // 打印相关信息
                        String myOutsideIp = new String(packet.getData(), 0, packet.getLength() - 4);
                        int myOutsidePort = ByteTool.byteToInt(Arrays.copyOfRange(packet.getData(), packet.getLength() - 4, packet.getLength()));
                        System.out.println("外网相关信息：" + myOutsideIp + ":" + myOutsidePort);
                        System.out.println("内网相关信息：" + InetAddress.getLocalHost().getHostAddress() + ":" + socket.getLocalPort());

                    } else {
                        System.out.println("收到非ojbk消息(" + packet.getAddress().getHostAddress() + "),长度为:" + packet.getLength());
                    }

                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
